package com.slipper.codeless.resolve.support;

import com.slipper.constants.Constants;
import com.slipper.util.DatabaseUtil;

public class MysqlSqlBuilder extends CommonSqlBuilder {
    @Override
    public String getSymbol() {
        return DatabaseUtil.getSymbol(Constants.Database.MYSQL);
    }

    @Override
    public String getQueryRecursiveSql(String tableName, String pidName, String idName, String param) {
        return " select " + wrapSymbol(idName) + " from ( " +
                " select *, @id := concat(@id, ',', " + wrapSymbol(idName) + ") " +
                " from " +
                "     (select " + wrapSymbol(idName) + ", " + wrapSymbol(pidName) +
                "           from " + wrapSymbol(tableName) + " order by " + wrapSymbol(pidName) + ", " + wrapSymbol(idName) + ") a, " +
                "     (select @id := #{" + param + "}) b " +
                " where find_in_set(" + wrapSymbol(pidName) + ", @id) > 0 or find_in_set(" + wrapSymbol(idName) + ", @id) > 0 ) c";
    }

    @Override
    public String getQueryOrgLevelSql(String param) {
        return " select id from ( " +
                " select *, @id := concat(@id, ',', id) " +
                " from " +
                "     (select id, parent_id from sys_organization order by parent_id, id) a, " +
                "     (select @id := #{" + param + "}) b " +
                " where find_in_set(parent_id, @id) > 0 or find_in_set(id, @id) > 0 ) c";
    }
}
